package com.dwxt.mall.controller;

import com.dwxt.common.annotation.ValidateFiled;
import com.dwxt.common.annotation.ValidateGroup;
import com.dwxt.common.base.BaseResult;
import com.dwxt.mall.entity.ReceiptAddress;
import com.dwxt.mall.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/mall/address")
@RestController
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * showdoc
     *
     * @param id        可选 int 主键id
     * @param phone     必选 string 电话号码
     * @param address   必选 string 收货地址
     * @param username  必选 string 收货人姓名
     * @param isDefault 可选 int 是否是默认收货地址 0:否 1:是
     * @return {"status":200,"msg":"SUCCESS","data":null}
     * @catalog 商城/收货地址
     * @title 新增或修改收货地址
     * @description 新增或修改收货地址
     * @url /mall/address/createOrUpdateAddress
     */
    @RequestMapping("/createOrUpdateAddress")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, filedName = "phone", regStr = "^1[3|4|5|6|7|8|9][0-9]\\d{4,8}$"),
            @ValidateFiled(index = 0, filedName = "address", notNull = true),
            @ValidateFiled(index = 0, filedName = "username", notNull = true),
            //@ValidateFiled(index = 0, filedName = "isDefault", notNull = true)
    })
    public BaseResult createOrUpdateAddress(ReceiptAddress receiptAddress) {
        addressService.createOrUpdateAddress(receiptAddress);
        return BaseResult.ok();
    }

    /**
     * showdoc
     *
     * @return {"status":200,"msg":"SUCCESS","data":[{"id":2,"username":"yanlong","phone":"15123058497","address":"九龙坡","userId":"158502014539731","createTime":"2020-04-14T17:56:39.000+0000","isDefault":1}]}
     * @catalog 商城/收货地址
     * @title 查询所有的收货地址
     * @description 查询所有的收货地址
     * @url /mall/address/selectAllAddress
     */
    @RequestMapping("/selectAllAddress")
    public BaseResult selectAllAddress() {
        List<ReceiptAddress> addressList = addressService.selectAllAddress();
        return BaseResult.ok(addressList);
    }

    /**
     * showdoc
     *
     * @return {"status":200,"msg":"SUCCESS","data":{"id":2,"username":"yanlong","phone":"15123058497","address":"九龙坡","userId":"158502014539731","createTime":"2020-04-14T17:56:39.000+0000","isDefault":1}}
     * @catalog 商城/收货地址
     * @title 查询默认的收货地址
     * @description 查询默认的收货地址
     * @url /mall/address/selectDefaultAddress
     */
    @RequestMapping("/selectDefaultAddress")
    public BaseResult selectDefaultAddress() {
        ReceiptAddress address = addressService.selectDefaultAddress();
        return BaseResult.ok(address);
    }

    /**
     * showdoc
     *
     * @param id 必选 int 主键id
     * @return {"status":200,"msg":"SUCCESS","data":null}
     * @catalog 商城/收货地址
     * @title 删除收货地址
     * @description 删除收货地址
     * @url /mall/address/delete
     */
    @RequestMapping("/delete")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, notNull = true),
            //@ValidateFiled(index = 0, filedName = "isDefault", notNull = true)
    })
    public BaseResult delete(Integer id) {
        addressService.delete(id);
        return BaseResult.ok();
    }

    /**
     * showdoc
     *
     * @param id 必选 int 主键id
     * @return {"status":200,"msg":"SUCCESS","data":{"id":2,"username":"yanlong","phone":"15123058497","address":"九龙坡","userId":"158502014539731","createTime":"2020-04-14T17:56:39.000+0000","isDefault":1}}
     * @catalog 商城/收货地址
     * @title 查询特定的收货地址
     * @description 查询特定的收货地址
     * @url /mall/address/selectByKey
     */
    @RequestMapping("/selectByKey")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, notNull = true),
            //@ValidateFiled(index = 0, filedName = "isDefault", notNull = true)
    })
    public BaseResult selectByKey(Integer id) {
        ReceiptAddress address = addressService.selectByKey(id);
        return BaseResult.ok(address);
    }
}
